package DecertifiSys.service;

import DecertifiSys.untill.baseUntill.En_DecryptUntill;
import DecertifiSys.untill.baseUntill.HttpDataUntill;
import DecertifiSys.untill.baseUntill.MessageUntill;
import DecertifiSys.untill.baseUntill.YmlConfig;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
/**
 * Created by zhoumeineng on 2018/2/1.
 */
@Service
public class AccountImpl {

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    YmlConfig ymlConfig;

    @Autowired
    TokenImpl tokenImpl;

    /**
     * 账号登录
     * restTemplate.postForObject 向注册系统发送请求获取注册信息（账号 ，密码）
     * @param parmar 参数
     * */
    public JSONObject AccounLogin(JSONObject parmar){

        //post 请求获取 json 数据
        JSONObject result = HttpDataUntill.PostReqsuet(parmar,ymlConfig.getRegisteSysgetUser(),restTemplate);

        if (result.getString("respCode").equals("0000")){
            JSONObject jsonObject = result.getJSONObject("attach");

            // 加密的密码
            String  EncryptPwd =  jsonObject.getString("password");
            // 密码相同 登录成功
            if (En_DecryptUntill.Base64Encrypt(parmar.getString("password")).equals(EncryptPwd)){
                return tokenImpl.bringToken(parmar.getString("phoneNum"),jsonObject.getString("uid"),"");

            }else {
                return  MessageUntill.messageBody(null,"0002","输入的密码与账号不匹配");
            }
        }else {
            return  MessageUntill.messageBody(null,"0004","账号登录失败");
        }

    }


    /**
     *
     * 获取脸部图片
     * */
    public JSONObject getFaceImage(JSONObject param){

        JSONObject jsonObject = HttpDataUntill.PostReqsuet(param,ymlConfig.getRegisteSysgetUser(),restTemplate);
        if (jsonObject==null||!jsonObject.getString("respCode").equals("0000")){
            return MessageUntill.messageBody(null,"0007","手机输入有误");
        }

        JSONObject attach = jsonObject.getJSONObject("attach");

        JSONObject jsonObject2 = HttpDataUntill.PostReqsuet(attach,ymlConfig.getRegiseSysgetUserInfo(),restTemplate);

        if (jsonObject2==null||!jsonObject2.getString("respCode").equals("0000")){
            return MessageUntill.messageBody(null,"0005","还未开通刷脸功能");
        }

        JSONObject attach2 = jsonObject2.getJSONObject("attach");
        if (attach2.getString("face")==null|attach2.getString("face").trim().equals("")){
           return MessageUntill.messageBody(null,"0005","还未开通刷脸功能");
        }else {
            return jsonObject2;
        }
    }


    /**
     *
     * 刷脸登录
     * @param parmar 前端传递的参数
     * */
    public JSONObject faceLogin(JSONObject parmar){
        //post 请求获取 json 数据
        JSONObject jsonObject = HttpDataUntill.PostReqsuet(parmar,ymlConfig.getRegisteSysgetUser(),restTemplate);
        if (jsonObject==null||!jsonObject.getString("respCode").equals("0000")){
            return MessageUntill.messageBody(null,"0007","手机输入有误");
        }
        JSONObject result = jsonObject.getJSONObject("attach");
        return tokenImpl.bringToken(result.getString("phoneNum"),result.getString("uid"),"");
    }


}
